package cnn.test.controller;

import cnn.sys.RestSession;
import cnn.user.entity.UserDTO;
import cnn.user.entity.UserQTO;
import cnn.user.service.UserService;
import com.alibaba.fastjson2.JSONObject;
import cnn.topfox.annotation.TokenOff;
import cnn.topfox.common.AppContext;
import cnn.topfox.sql.Condition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController  {
    @Autowired
    UserService userService;

    @TokenOff
    @GetMapping("/test1")
    public Object test1(UserQTO userQTO) {
        //例1: 根据id查询, 通过第2个参数传false 就不读取一二级缓存了
        userService.getObject(1, false);

        //例2: 根据多个id查询, 要查询的id放入Set容器中
        Set setIds = new HashSet();
        setIds.add(1);
        setIds.add(2);
        //通过第2个参数传false 就不读取一二级缓存了
        List<UserDTO> list = userService.listObjects(setIds, false);

        //例3: 通过QTO 设置不读取缓存
        list = userService.listObjects(
                userQTO.readCache(false) //禁用从缓存读取(注意不是读写) readCache 设置为 false, 返回自己(QTO)
        );
        //或者写成:
        userQTO.readCache(false);
        list = userService.listObjects(userQTO);

        //例4: 通过条件匹配器Condition 设置不读取缓存
        list = userService.listObjects(
                Condition.create()     //创建条件匹配器
                        .readCache(false)  //禁用从缓存读取
        );

        //例5: 通过查询构造器 EntitySelect 设置不读取缓存
        list = userService.listObjects(
                userService.select()//创建 EntitySelect
                        .where()
                        .readCache(false)//禁用从缓存读取
        );
        return list;
    }

    @TokenOff
    @GetMapping("/test2")
    public UserDTO test2() {
        UserDTO user1 = userService.getObject(1);//查询后 会放入一级 二级缓存
        UserDTO user2 = userService.getObject(1);//会从一级缓存中获取到

        user2.setName("张三2");
        //会从一级缓存中获取到, 拿到的是修改过后的对象(张三2)
        UserDTO user3 = userService.getObject(1);

        //不从缓存拿数据, 即始终从数据库中获取
        UserDTO user4 = userService.getObject(1,false);

        UserDTO user5 = userService.getObject(
                Condition.create().eq("name","张三")
        );


        return user3;
    }

    @TokenOff
    @PostMapping("/test3")
    public void test3(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("####parameterMap: "+parameterMap);

        RestSession restRession = AppContext.getRestSession();

        JSONObject requestData = restRession.getRequestData();
        System.out.println("####requestData: "+requestData);
    }
}
